<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Creator {

	function create_calendar($tahun,$bulan,$url,$cal_data = array()){
		$CI =& get_instance();

		$CI->config->load('calendar');
		$CI->load->library('calendar',array(
			'start_day' => 'monday',
			'show_next_prev' => true,
			'next_prev_url' => $url,
			'template' => $CI->config->item('calendar_template'),
		));
		$calendar = $CI->calendar->generate($tahun, $bulan, $cal_data);

		return $calendar;
	}

	function create_kunjungan_block($kunjungan, $content_only = false, $edit_teknisi = false, $edit_testimoni = false, $edit_id = false){
		$CI =& get_instance();

		$block = new Block();
		if($content_only) $block->set_content_only();

		$block->add_hidden_field('id',$kunjungan->get_id());

		if($edit_id)
			$block->add_editable_data('flex_id','ID Kunjungan',$kunjungan->get_flex_id());
		else $block->add_data('ID Kunjungan',$kunjungan->get_flex_id());

		if($edit_teknisi)
			$block->add_editable_teknisi($kunjungan->get_teknisi(),'Teknisi yang bertugas');
		else $block->add_data('Teknisi',$kunjungan->get_teknisi()->get_nama());

		if($edit_testimoni)
			$block->add_editable_testimonial($kunjungan->get_testimoni());
		else {
			if($kunjungan->get_testimoni() == '')
				$block->add_data('Testimonial','Belum ada testimoni dari customer');
			else $block->add_data('Testimonial',$kunjungan->get_testimoni());
		}

		if($edit_teknisi || $edit_testimoni || $edit_id){
			$block->set_form('kunjungan/ajax_edit_kunjungan/'.$kunjungan->get_id(),'edit_teknisi');
			$block->add_menu('edit','','class = edit_menu');
		}

		return $block;
	}

	function create_checklist_block($checklist, $content_only = false, $editable = true){
		$CI =& get_instance();

		$block = new Block();
		if($content_only) $block->set_content_only();

		$block->set_form(
				$CI->uri->segment(1).'/ajax_edit_checklist/'.$checklist->get_id(),
				'edit_chklist_'.$checklist->get_id()
			)->add_device_info($checklist);

		if($editable == true){
			$block->add_menu('edit','','class = edit_menu')
				->add_menu('x',$CI->uri->segment(1).'/ajax_hapus_checklist/'.$checklist->get_id(),'class = delete_menu');
		}

		$detail = $checklist->get_detail_checklist();
		foreach($detail as $d){
			$block->add_editable_pengecekan($d);
		}
		$block->add_new_pengecekan();

		return $block;
	}

	function create_checklist_blocks($checklists,$editable = true){
		$CI =& get_instance();

		foreach($checklists as $cl){
			$block = $CI->creator->create_checklist_block($cl,false,$editable);
			$CI->main_content->add_block($block);
		}
	}

	function create_detail_kunjungan_page($kunjungan){
		$CI =& get_instance();

		$kunjungan_id = $kunjungan->get_id();
		$teknisi = $kunjungan->get_teknisi();
		$customer = $kunjungan->get_customer();
		$checklists = $kunjungan->get_checklists();
		$tanggal = $kunjungan->get_tanggal();

		if(models\Current_user::previlege('Administrator|Agen')){

			$CI->main_content->add_block($CI->creator->create_kunjungan_block($kunjungan,false,true,false,true));
			$CI->main_content->add_custom('p','Terdapat '.count($checklists).' checklist hari ini');

			if(models\Current_user::previlege('Administrator')){
				$CI->headbar->add_menu('tambah_checklist','+ checklist',"customer/tambah_checklist/$kunjungan_id");
				$CI->creator->create_checklist_blocks($checklists);
			} else if(models\Current_user::previlege('Agen'))
				$CI->creator->create_checklist_blocks($checklists,false);

			$CI->headbar->add_menu('delete_kunjungan','Delete',"customer/ajax_hapus_kunjungan/$kunjungan_id");
			
		} else if(models\Current_user::previlege('Teknisi')){

			$CI->main_content->add_block($CI->creator->create_kunjungan_block($kunjungan));
			$CI->main_content->add_custom('p','Terdapat '.count($checklists).' checklist hari ini');

			if($teknisi->get_id() == $CI->current_user->get_id()){
				$CI->headbar->add_menu('tambah_checklist','+ checklist',"customer/tambah_checklist/$kunjungan_id");
				$CI->headbar->add_menu('delete_kunjungan','Delete',"customer/ajax_hapus_kunjungan/$kunjungan_id");
				$CI->creator->create_checklist_blocks($checklists);
			} else $CI->creator->create_checklist_blocks($checklists,false);

		} else if(models\Current_user::previlege('Customer|Perusahaan')){

			$CI->main_content->add_block($CI->creator->create_kunjungan_block($kunjungan,false,false,true));
			$CI->main_content->add_custom('p','Terdapat '.count($checklists).' checklist hari ini');
			$CI->creator->create_checklist_blocks($checklists,false);

		}
	}

	function create_daftar_kunjungan($array_of_kunjungan, $fields = array()){
		$CI =& get_instance();

		$legend_block = $CI->main_content->add_block();
		$legend_block->set_class('header_block');
		$legend_block->add_grid(1,false,true)->add_custom('p','ID');

		foreach ($fields as $field) {
			if($field == 'customer') $legend_block->add_grid(3)->add_custom('p','Customer');
			if($field == 'alamat') $legend_block->add_grid(3)->add_custom('p','Alamat');
			if($field == 'checklists') $legend_block->add_grid(1)->add_custom('p','Cls');
			if($field == 'teknisi') $legend_block->add_grid(3)->add_custom('p','Teknisi');
			if($field == 'testimonial') $legend_block->add_grid(3)->add_custom('p','Testimonial');
		}

		$block = new Block();
		foreach($array_of_kunjungan as $k){

			$tanggal = $k->get_tanggal();
			$tanggal_link = $tanggal->format('Y/m/d');

			$username_customer = $k->get_customer()->get_username();
			$username_teknisi = $k->get_teknisi()->get_username();
			$jml_checklist = count($k->get_checklists());
			$alamat = $k->get_customer()->get_alamat().' '.
				$k->get_customer()->get_kota().' '.
				$k->get_customer()->get_propinsi();

			$block = $CI->main_content->add_block();
			$block->add_grid(1)
				->set_alpha(false)
				->set_omega(true)
				->set_class('link')
				->add_custom('p',anchor("customer/checklist/$username_customer/$tanggal_link",$k->get_flex_id()));

			foreach ($fields as $field) {
				if($field == 'customer') $block->add_grid(3)->add_info($username_customer.' - '.$k->get_customer()->get_nama());
				if($field == 'alamat') $block->add_grid(3)->add_custom('p',$alamat);
				if($field == 'checklists') $block->add_grid(1)->add_custom('p',$jml_checklist);
				if($field == 'teknisi') $block->add_grid(3)->add_info($username_teknisi.' - '.$k->get_teknisi()->get_nama());
				if($field == 'testimonial') $block->add_grid(3)->add_info($k->get_testimoni());
			}

			$block->set_class('kunjungan_block');

		}
		$block->set_class('last_kunjungan_block');
	}

	function create_tambah_kunjungan_block($tanggal,$teknisi_id = ''){
		$CI =& get_instance();

		$block = $CI->main_content->add_block('Tambah Kunjungan');

		if($teknisi_id == '')
			$block->add_dropdown_field('teknisi','Teknisi',models\Teknisi::get_dropdown_options());
		else $block->add_hidden_field('teknisi',$teknisi_id);

		$block->add_hidden_field('tanggal',$tanggal->format('Y-m-d'))
			->add_dropdown_field('customer','Customer',models\Customer::get_dropdown_options())
			->set_form("kunjungan/ajax_tambah_kunjungan",'tambah_kunjungan')
			->set_style('display: none')
			->set_id('tambah_kunjungan_block');
	}

}
